*****************************************************************************
*																			*
*						Campaign Panel Book Do-File Chapter 2 -	TV-Content 2013										*
*																			*
*****************************************************************************

cd "${data}"
use ZA5705_v1-0-0.dta, clear		
		
drop if filter_bei==0		//contribution level
drop if B02==2			//no thematical classification possible /opening credits, greetings etc./


tab B02 B06, m	// 100% of all missings in B06 cannot be thematically classified 


fre B06		//sadly in one category: no politics/
			//no federal-level political actor, but we would like to be able to 
			//distinguish these two
fre B10
tab B06 B10, m

*---------------------------------------
* Date
*---------------------------------------

gen date = mdy(V04, V05, year)  //in Stata date format
format date %d  //make readable				
					
					
*-------------------------------------
* Evaluation of Contributions
*------------------------------------------

recode B20a-B20k (0=.) (1=1) (2=0.5) (3 9=0) (4=-0.5) (5=-1) (.=.)
lab val B20a-B20k


 *CDU
egen toncdurtl = rowmean(B20a B20b B20c) if  filter_dpol==1 &  V02=="RTL aktuell" 
egen toncdusat = rowmean(B20a B20b B20c) if  filter_dpol==1 &  V02=="Sat.1 Nachrichten" 
egen toncduard = rowmean(B20a B20b B20c) if  filter_dpol==1 &  V02=="Tagesschau" 
egen toncduzdf = rowmean(B20a B20b B20c) if  filter_dpol==1 &  V02=="heute" 

*SPD
egen tonspdrtl = rowmean(B20d B20e) if  filter_dpol==1 &  V02=="RTL aktuell" 
egen tonspdsat = rowmean(B20d B20e) if  filter_dpol==1 &  V02=="Sat.1 Nachrichten" 
egen tonspdard = rowmean(B20d B20e) if  filter_dpol==1 &  V02=="Tagesschau" 
egen tonspdzdf = rowmean(B20d B20e) if  filter_dpol==1 &  V02=="heute"

*FDP
egen tonfdprtl = rowmean(B20f B20g) if  filter_dpol==1 &  V02=="RTL aktuell"
egen tonfdpsat = rowmean(B20f B20g) if  filter_dpol==1 &  V02=="Sat.1 Nachrichten"
egen tonfdpard = rowmean(B20f B20g) if  filter_dpol==1 &  V02=="Tagesschau"
egen tonfdpzdf = rowmean(B20f B20g) if  filter_dpol==1 &  V02=="heute"

	
*The Greens
egen tongrurtl = rowmean(B20h B20i) if  filter_dpol==1 &  V02=="RTL aktuell"
egen tongrusat = rowmean(B20h B20i) if  filter_dpol==1 &  V02=="Sat.1 Nachrichten"
egen tongruard = rowmean(B20h B20i) if  filter_dpol==1 &  V02=="Tagesschau"
egen tongruzdf = rowmean(B20h B20i) if  filter_dpol==1 &  V02=="heute"

*The Left
egen tonlinkrtl = rowmean(B20j B20k) if  filter_dpol==1 &  V02=="RTL aktuell"
egen tonlinksat = rowmean(B20j B20k) if  filter_dpol==1 &  V02=="Sat.1 Nachrichten"
egen tonlinkard = rowmean(B20j B20k) if  filter_dpol==1 &  V02=="Tagesschau"
egen tonlinkzdf = rowmean(B20j B20k) if  filter_dpol==1 &  V02=="heute"



sum ton*


*----------------------------------------
* Aggregate everything after one day
*----------------------------------------

/*
beginning at this point:
variables with "dpol" (e.g., lg1dpolbias) refer to federal policy 
*/

collapse (mean) ton*, by(date)

expand 2 if  date==date("21sep2013", "DMY"), gen(expanded)

/* Collection of TV-news ends before end of WKP. Since we want every person to
receive the average mediatone in the week before their interview, we can add
another day.
Solution:
Copy the last observation and stick to bottom. Possible using expand-command. 
Set values on all variables to missing for this additional day*/
	


gen n = _n			//generate running number
order n				//right at the beginning of the dataset
					
replace date = date+1 if  n==92		//adjust date for new observations

foreach var of varlist ton* {
replace `var' = . if  n>=92
}

*-----------------------------------------------
* Create lags up to 7 days in advance
*------------------------------------------------

foreach s in ard zdf rtl sat {
foreach num of numlist 1/7 {
foreach partei in cdu spd fdp gru link {
	gen lg`num'ton`partei'`s' = ton`partei'`s'[_n-`num']		
}
}  
}


*-------------------------------------
* Aggregate everything according to panel wave
*-------------------------------------------

* To do this, first create wave-variable

/* to fit to question wording in election campaign panel (reception last week)
use times 1 week displaced

election campaign panel begins on June 20th, media content analysis only on June 23rd,
therefore begin with wave 2
*/

gen welle = 2 if    date>=date("11jul2013", "DMY") ///
	& date<=date("21jul2013", "DMY")
replace welle = 3 if    date>=date("25jul2013", "DMY") ///
	& date<=date("04aug2013", "DMY")
replace welle = 4 if    date>=date("08aug2013", "DMY") ///
	& date<=date("18aug2013", "DMY")
replace welle = 5 if    date>=date("26aug2013", "DMY") ///
	& date<=date("05sep2013", "DMY")
replace welle = 6 if    date>=date("09sep2013", "DMY") ///
	& date<=date("14sep2013", "DMY")
replace welle = 7 if    date>=date("17sep2013", "DMY") ///
	& date<=date("27sep2013", "DMY") //wave seven is not covered entirely  

	
* collapse by wave
collapse (mean) lg*, by(welle)  
//now: each row one wave

foreach s in ard zdf rtl sat {
foreach partei in cdu spd fdp gru link {
egen panelfinalton`s'`partei' = rowmean(lg?ton`partei'`s') 
}
}


*---------------------------------------------------------
* Figure 3: Partisan bias of TV newscasts in 2013
*-------------------------------------------------------------

graph set window fontface "Times New Roman"

graph twoway connected panelfinaltonardcdu panelfinaltonzdfcdu ///
	panelfinaltonrtlcdu panelfinaltonsatcdu welle ///
	if welle~=1 & welle~=7, sort ///
	xlabel(2/6)  scheme(s1mono) ///
	title("CDU/CSU", ///
	size(normal) margin(medium)) ///
	legend(label(1 ARD) label(2 ZDF) label(3 RTL) label(4 Sat 1) ///
	position(6) region(lwidth(none)) size(vsmall)) xtitle(" ", margin(medium)) ///
	ylabel(-1(0.5)0.5, angle(horizontal)) name(cdu, replace)
	
	
graph twoway connected panelfinaltonardspd panelfinaltonzdfspd ///
	panelfinaltonrtlspd panelfinaltonsatspd welle ///
	if welle~=1 & welle~=7, sort ///
	xlabel(2/6) scheme(s1mono) ///
	title("SPD", ///
	size(normal) margin(medium)) legend(off) xtitle(" ", margin(medium)) ///
	ylabel(-1(0.5)0.5,angle(horizontal)) name(spd, replace)
	
	
graph twoway connected panelfinaltonardfdp panelfinaltonzdffdp ///
	panelfinaltonrtlfdp panelfinaltonsatfdp welle ///
	if welle~=1 & welle~=7, sort ///
	xlabel(2/6) scheme(s1mono) ///
	title("FDP", ///
	size(normal) margin(medium)) legend(off) xtitle("Wave", margin(medium)) ///
	ylabel(-1(0.5)0.5,angle(horizontal)) name(fdp, replace)
	
	
grc1leg cdu spd fdp, row(3) iscale(.5) imargin(45 45 0 0) ycommon /// combine graphs in one window
	scheme(s1mono) legendfrom(cdu) 
	
graph export "${path}\Reproducible Do-Files\Chapter 2\Figures\medienton2013_notitle_nocaption 2.tif", ///
	width(2000) height(2500) replace

graph export "${path}\Reproducible Do-Files\Chapter 2\Figures\medienton2013_notitle_nocaption.wmf", ///
	replace

